function checkFacebooklike(mac, id_campaign) {
    if ($(".swiper-slide-active .ui-facebooklike").length > 0) {//neu co nut like
        var facebook_url = $(".swiper-slide-active").find('.fb-like').data('href');
        $.ajax({
            url: "check-facebooklike",
            type: "GET",
            data: {
                mac: mac,
                facebook_url: facebook_url,
                id_campaign: id_campaign
            },
            success: function (data) {
                data = JSON.parse(data);
                //console.log(data);
                if (data != "200") {
                    $(".swiper-slide-active .ui-facebooklike").html('<span>Truy cập miễn phí</span>')
                            .addClass("ui-facebookliked ui-btn ui-btn-block")
                            .removeClass('ui-facebooklike')
                            .removeAttr('id')
                            .attr('data-fb', JSON.stringify(data[0]));

                    $(".ui-facebookliked").on('tap', function () {
                        $(".ui-facebookliked").off('tap');
                        var data = $(this).data('fb');
                        Toast.showWait();
                        $.ajax({
                            type: "get",
                            url: baseurl + "insert-like-fb",
                            data: {
                                mac: mac,
                                nasid: nasid,
                                facebook_url: data.facebook_url,
                                id_campaign: data.id_campaign,
                                fbid: data.fbid,
                                email: data.email,
                                fullname: data.fullname,
                                name_device: name_device,
                                landing_page: landing_page,
                                id_place: id_place,
                                type_time_session: type_time_session
                            },
                            success: function (data) {
                                if (data == '200') {
                                    Toast.showMessage({typeMessage: 'success'});
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.showWait({text: "Truy cập internet...", width: "150px"});
                                        location.href = authentication_link;
                                    }, 1000);
                                } else {
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.show({text: "Có lỗi hệ thống."});
                                    }, 300);
                                }
                            },
                            error: function () {
                                Toast.close();
                                Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
                            }
                        });
                    });
                }
            }
        });
    }
}

function checkEmail(mac, id_campaign) {
    if ($(".swiper-slide-active .email").length > 0) {//neu co nut Email
        $.ajax({
            url: "check-email",
            type: "GET",
            data: {
                mac: mac,
                id_campaign: id_campaign
            },
            success: function (data) {
                data = JSON.parse(data);
                if (data != "200") {
                    $(".swiper-slide-active .email").html('<span>Truy cập miễn phí</span>')
                            .attr("class", "ui-email-submited ui-btn ui-btn-block")
                            .removeAttr('id')
                            .attr('data-email', JSON.stringify(data[0]));

                    $(".target.email").off("tap");
                    $(".ui-email-submited").off('tap');
                    $(".ui-email-submited").on('tap', function () {
                        var data = $(this).data('email');
                        Toast.showWait();
                        $.ajax({
                            type: "get",
                            url: baseurl + "campaign-email",
                            data: {
                                email: data.email,
                                mac: mac,
                                nasid: nasid,
                                name_device: name_device,
                                id_campaign: id_campaign,
                                landing_page: landing_page,
                                id_place: id_place,
                                type_time_session: type_time_session
                            },
                            success: function (data) {
                                if (data == '200') {
                                    Toast.showMessage({typeMessage: 'success'});
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.showWait({text: "Truy cập internet...", width: "150px"});
                                        location.href = authentication_link;
                                    }, 1000);
                                } else {
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.show({text: "Có lỗi hệ thống."});
                                    }, 300);
                                }
                            },
                            error: function () {
                                Toast.close();
                                Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
                            }
                        });
                    });
                }
            }
        });
    }
}

function checkPhone(mac, id_campaign) {
    if ($(".swiper-slide-active .phone").length > 0) {//neu co nut Phone
        $.ajax({
            url: "check-phone",
            type: "GET",
            data: {
                mac: mac,
                id_campaign: id_campaign
            },
            success: function (data) {
                if (data != "200") {
                    data = JSON.parse(data);
                    $(".swiper-slide-active .phone").html('<span>Truy cập miễn phí</span>')
                            .attr("class", "ui-phone-submited ui-btn ui-btn-block")
                            .removeAttr('id')
                            .attr('data-phone', JSON.stringify(data[0]));

                    $(".target.phone").off("tap");
                    $(".ui-phone-submited").off('tap');
                    $(".ui-phone-submited").on('tap', function () {
                        var data = $(this).data('phone');
                        Toast.showWait();
                        $.ajax({
                            type: "get",
                            url: baseurl + "campaign-phone",
                            data: {
                                phone: data.mobile,
                                mac: mac,
                                nasid: nasid,
                                name_device: name_device,
                                id_campaign: id_campaign,
                                landing_page: landing_page,
                                id_place: id_place,
                                type_time_session: type_time_session
                            },
                            success: function (data) {
                                if (data == '200') {
                                    Toast.showMessage({typeMessage: 'success'});
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.showWait({text: "Truy cập internet...", width: "150px"});
                                        location.href = authentication_link;
                                    }, 1000);
                                } else {
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.show({text: "Có lỗi hệ thống."});
                                    }, 300);
                                }
                            },
                            error: function () {
                                Toast.close();
                                Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
                            }
                        });
                    });
                }
            }
        });
    }
}
var loginwifi_checked = false;
function checkLoginWifi(mac, id_campaign) {
    if ($(".swiper-slide-active .login").length > 0 && $(".swiper-slide-active .ui-login-submited").length == 0 && (loginwifi_checked == false || loginwifi_checked == undefined)) {//neu co nut Phone
        Toast.showWait();
        $.ajax({
            url: "check-wifipassword",
            type: "GET",
            timeout: 5000,
            data: {
                mac: mac,
                id_campaign: id_campaign
            },
            success: function (data) {
                loginwifi_checked = true;
                if (data != "200") {
                    data = JSON.parse(data);
                    var $login = $(".swiper-slide-active .login").clone();
                    $(".swiper-slide-active .login").parents('.ui-block').find('.element').hide();
                    $(".swiper-slide-active .login").hide();

                    $login.html('<span>Truy cập miễn phí</span>')
                            .attr('data-login', JSON.stringify(data[0]))
                            .addClass('ui-login-submited').removeClass('login');
                    $(".swiper-slide-active .login").after($login);
                    Toast.close();
                    $(".ui-login-submited").off('tap');
                    $(".ui-login-submited").on('tap', function () {
                        var data = $(this).data('login');
                        //console.log(data);
                        Toast.showWait();
                        $.ajax({
                            type: "get",
                            url: baseurl + "campaign-wifipassword",
                            timeout: 5000,
                            data: {
                                mac: mac,
                                nasid: nasid,
                                name_device: name_device,
                                id_campaign: id_campaign,
                                landing_page: landing_page,
                                id_place: id_place,
                                password: data.password,
                                type_time_session: type_time_session
                            },
                            success: function (data) {
                                if (data == '200') {
                                    Toast.showMessage({typeMessage: 'success'});
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.showWait({text: "Truy cập internet...", width: "150px"});
                                        location.href = authentication_link;
                                    }, 1000);
                                } else if (data == '201') {
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.show({text: "Thiết lập mật khẩu hệ thống có lỗi."});
                                    }, 300);
                                } else {
                                    setTimeout(function () {
                                        Toast.close();
                                        Toast.show({text: "Mật khẩu không chính xác."});
                                    }, 300);
                                }
                            },
                            error: function () {
                                Toast.close();
                                Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
                            }
                        });
                    });
                } else {
                    Toast.close();
                }
            },
            error: function () {
                Toast.close();
            }
        });
    }
}

function saveViewLog(id_campaign) {
    if (id_campaign != undefined && id_campaign != '' && codefb == 0) {
        if ($.inArray(id_campaign, viewlog) < 0) {
            viewlog.push(id_campaign);
            $.ajax({
                url: baseurl + "insert-log-access",
                type: "GET",
                data: {
                    mac: mac,
                    nasid: nasid,
                    id_campaign: id_campaign,
                    code: codefb,
                    id_place: id_place
                },
                success: function (data) {
                }
            });
        }
    }
}

function joinWifiFree(custom_landing_page) {
    var url = custom_landing_page == undefined ? landing_page : custom_landing_page;
    Toast.showWait();
    $.ajax({
        type: "GET",
        url: baseurl + "campaign-free",
        timeout: 5000,
        data: {
            mac: mac,
            nasid: nasid,
            name_device: name_device,
            id_campaign: id_campaign,
            landing_page: url,
            id_place: id_place,
            type_time_session: type_time_session
        },
        success: function (data) {
            if (data == '200') {
                Toast.showMessage({typeMessage: 'success'});
                setTimeout(function () {
                    Toast.close();
                    Toast.showWait({text: "Truy cập internet...", width: "150px"});
                    location.href = authentication_link;
                }, 1000);
            } else {
                setTimeout(function () {
                    Toast.close();
                    Toast.show({text: "Có lỗi hệ thống."});
                }, 300);
            }
        },
        error: function () {
            Toast.close();
            Toast.show({text: "Xảy ra lỗi, vui lòng thử lại."});
        }
    });
}

function checkinFacebook(success) {
    $.ajax({
        url: baseurl + 'default/index/facebookcheckin',
        success: function (data) {
            data = JSON.parse(data);
            console.log(data);
            if (data.status == '200') {
                success();
            }
            else if (data.status == '201') {
                if (data.result.error.code == '506') {//Đã checkin trước đó rồi
                    success();
                }
            }
            else {
                Toast.close();
                Toast.show({text: "Checkin xảy ra lỗi, vui lòng thử lại"});
            }
        }, error: function () {
            Toast.close();
            Toast.show({text: "Checkin xảy ra lỗi, vui lòng thử lại"});
        }
    });
}

/*
 *@Lưu trạng thái click vào session khi có hành động refresh hoặc call back từ 
 *trang khác trở về sẽ sử dụng ở biến last_action 
 */
function set_last_action(action, success) {
    slide_index = swiper != null ? swiper.activeIndex : 0;
    $.ajax({
        url: baseurl + 'default/index/lastaction',
        data: {action: action, id_campaign: id_campaign, slide_index: slide_index},
        success: function (data) {
            success(data);
        },
        error: function () {
            Toast.showWait({text: "Có lỗi, vui lòng thử lại."});
        }
    });
}